package controller

import (
	"cloudmusic/model"
	. "cloudmusic/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

func Hello(c *gin.Context) {
	Response(c, http.StatusOK, "Cloud Music Resultful Api Init Success", gin.H{
		"now_time": TimestampToDate(time.Now().Unix()),
	})
}
func PlayList(c *gin.Context) {
	if model.MusicList.Expire < time.Now().Unix() {
		_ = model.GetMusicList()
	}
	Response(c, http.StatusOK, "获取成功!", model.MusicList.List)
}
func Play(c *gin.Context) {
	objectId := c.Query("id")
	songId, _ := strconv.ParseInt(objectId, 10, 64)
	music := model.Music{
		Id: songId,
	}
	err := music.GetMusicById()
	if err != nil {
		Response(c, http.StatusBadRequest, "获取失败!", nil)
		return
	}
	_ = model.IncMusicPlayTimes(music.Id)
	presignedURL, err := MinIOClient.PresignedGetObject(music.Type, music.GetOssName()+"."+music.Type, 1000*time.Second, nil)
	imageURL, err := MinIOClient.PresignedGetObject("jpg", music.GetOssName()+".jpg", 1000*time.Second, nil)
	singerImg, err := MinIOClient.PresignedGetObject("jpg", "jaychou.jpg", 1000*time.Second, nil)
	if err != nil {
		Response(c, http.StatusBadRequest, "获取失败!", nil)
		return
	}
	Response(c, http.StatusOK, "获取成功!", gin.H{
		"mp3":       presignedURL.String(),
		"img":       imageURL.String(),
		"singerImg": singerImg.String(),
		"lyr":       music.Lyric,
	})
}
